﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ciripescu_WebRole.BL;

namespace Ciripescu_WebRole
{
    public partial class resetpass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                divErrorSend.InnerHtml = String.Empty;
                divErrorSetPass.InnerHtml = String.Empty;
            }
            if (!IsPostBack &&
                Request.QueryString["username"] != null &&
                Request.QueryString["confirm"] != null)
            {

                UserManager um = new UserManager();
                Ciripescu_WebRole.Entities.User usr =  um.GetItem(Request.QueryString["username"]);
                
                if (usr == null) // User doesn't exits
                {
                    ShowError.ShowErrorMessage(Properties.Resources.ErrInvalidUser,Context);
                    return;
                }

                if (Utils.GetPasswordResetKey(usr.Password) == Request.QueryString["confirm"])
                {
                    // user has clicked the link in his email so show him the change email form
                    panSend.Visible  = false;
                    panReset.Visible = true; 
                }
                else
                {
                    ShowError.ShowErrorMessage(Properties.Resources.ErrResetLinkNotV,Context);
                    return;
                }
            }
            else
            {
                panReset.Visible = false;
                panSend.Visible  = true;
            }
        }


        protected void btnSendMail_Click(object sender, ImageClickEventArgs e)
        {
            //check username or email
            UserManager um = new UserManager();
            Ciripescu_WebRole.Entities.User usr = null;

            if (txtEmailOrUsername.Text.Contains('@'))
                usr = um.GetItemByEmail(txtEmailOrUsername.Text);
            else
                usr = um.GetItem(txtEmailOrUsername.Text);

            if (usr == null)
            {
                divErrorSend.InnerHtml = Properties.Resources.ErrUserNotFound;
                return;
            }
            else
            {
                //send mail and tell user that mail was send
                MailQueue mq = new MailQueue();
                mq.SendResetPassMail(usr.Email, usr.PartitionKey, Utils.GetPasswordResetKey(usr.Password));
                divErrorSend.InnerHtml = Properties.Resources.EmailSend;
            }
        }

        protected void btnSaveNewPass_Click(object sender, ImageClickEventArgs e)
        {
            UserManager um = new UserManager();
            Ciripescu_WebRole.Entities.User usr = um.GetItem(Request.QueryString["username"]);

            if (usr == null) // User doesn't exits
            {
                ShowError.ShowErrorMessage(Properties.Resources.ErrInvalidUser, Context);
                return;
            }

            if (Utils.GetPasswordResetKey(usr.Password) == Request.QueryString["confirm"])
            {
                // user has clicked the link in his email so show him the change email form
                if (txtNewPass.Text.CompareTo(txtNewPass2.Text) == 0)
                {
                    usr.Password = Utils.MD5Crypt(txtNewPass.Text);
                    um.Save(usr);
                    Response.Redirect("/");    
                }
                else
                    divErrorSetPass.InnerHtml = Properties.Resources.ErrNoPassMatch;
            }
            else
            {
                ShowError.ShowErrorMessage(Properties.Resources.ErrResetLinkNotV, Context);
                return;
            }
        }
    }
}
